const cloud = require('wx-server-sdk')

cloud.init()

const SUCCESS = 0;
const BAD_PARAM = -1;

exports.main = async (event) => {
  const db = cloud.database();
  const content = event.content;

  if (content == null) {
    return {
      code: BAD_PARAM
    };
  }

  try {
    const parsedContent = JSON.parse(content);
    await db.collection('cards').add({
      data: parsedContent
    });
    return {
      code: SUCCESS
    };
  } catch (e) {
    return {
      code: BAD_PARAM
    };
  }
}
